package com.sundae.smarthomedemo.Activitys;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

import com.sundae.smarthomedemo.Adapter.WelcomePageAdapter;
import com.sundae.smarthomedemo.R;
import com.sundae.smarthomedemo.Utils.ToastUtils;

import java.util.ArrayList;

public class WelcomeActivity extends BaseActivity {

    private LayoutInflater inflater;
    private ArrayList<View> viewArrayList = new ArrayList<>();
    private WelcomePageAdapter welcomePageAdapter;

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        initView();

    }


    private void initView()
    {
        inflater = LayoutInflater.from(WelcomeActivity.this);
        View view1 = inflater.inflate(R.layout.welcome_page1 , null);
        View view2 = inflater.inflate(R.layout.welcome_page2 , null);
        View view3 = inflater.inflate(R.layout.welcome_page3 , null);
        viewArrayList.add(view1);
        viewArrayList.add(view2);
        viewArrayList.add(view3);

        welcomePageAdapter = new WelcomePageAdapter(viewArrayList);

        viewPager = (ViewPager) findViewById(R.id.welcome_page);
        viewPager.setAdapter(welcomePageAdapter);

        view3.findViewById(R.id.enter_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private long lastTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            long now = System.currentTimeMillis();
            if(now - lastTime > 2000)
            {
                ToastUtils.showShortToast(getApplicationContext() , "再按一次推出");
                lastTime = now;
                return true;
            }else{
                MyActivityManager.removeAllActivity();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
